package work.mediway.mdm.controller.hierarchy;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.mediway.hos.base.model.BaseResponse;
import com.mediway.hos.log.annotation.OperLog;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;

import cn.hutool.core.collection.CollUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import work.mediway.mdm.core.annotation.OpsLog;
import work.mediway.mdm.core.enums.OpsLogTypeEnum;
import work.mediway.mdm.core.page.QueryRequest;
import work.mediway.mdm.core.transport.ErrorMsgVO;
import work.mediway.mdm.biz.entity.hierarchy.HosOrganization;
import work.mediway.mdm.biz.service.hierarchy.HosOrganizationService;

/**
 * <p>
 * 组织机构树 前端控制器
 * </p>
 *
 * @author ruoyi
 * @since 2022-08-25
 */
@SuppressWarnings("unchecked")
@Slf4j
@Api(tags = "组织架构-组织管理")
@ApiSupport(order = 20)
@RestController
@RequestMapping("/hosOrganization")
@Validated
@RequiredArgsConstructor
public class HosOrganizationController {

    private final HosOrganizationService hosOrganizationService;

    @ApiOperation(value = "新增组织")
    @ApiOperationSupport(order = 1)
    @OpsLog(value = "新增组织", type = OpsLogTypeEnum.INSERT)
    @OperLog(title = "组织架构-新增组织", content = "新增组织")
    @PostMapping
    public BaseResponse<ErrorMsgVO> save(@Valid @RequestBody HosOrganization hosOrganization) {
        ErrorMsgVO errorMsgVO = this.hosOrganizationService.saveHosOrganization(hosOrganization);
        return BaseResponse.success(errorMsgVO);
    }

    @ApiOperation(value = "查询组织")
    @ApiOperationSupport(order = 3)
    @ApiImplicitParam(name = "tree", value = "是否树形展示", paramType = "query", allowableValues = "true,false")
    @OpsLog(value = "查询组织", type = OpsLogTypeEnum.SELECT)
    @GetMapping
    public BaseResponse<Object> find(HosOrganization hosOrganization, @RequestParam(defaultValue = "false") boolean tree, QueryRequest queryRequest) {
        Object result = this.hosOrganizationService.findHosOrganization(hosOrganization, tree, queryRequest);
        return tree ? BaseResponse.success(CollUtil.newArrayList(result)) : BaseResponse.success(((Page<Object>) result));
    }

    @ApiOperation(value = "更新组织")
    @ApiOperationSupport(order = 5)
    @OpsLog(value = "更新组织", type = OpsLogTypeEnum.UPDATE)
    @OperLog(title = "组织架构-更新组织", content = "更新组织")
    @PostMapping("update")
    public BaseResponse<HosOrganization> update(@Valid @RequestBody HosOrganization hosOrganization) {
        this.hosOrganizationService.updateHosOrganization(hosOrganization);
        return BaseResponse.success();
    }

    @ApiOperation(value = "删除组织")
    @ApiOperationSupport(order = 7)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orgCode", value = "组织code[多个以,分隔]", paramType = "query", required = true),
            @ApiImplicitParam(name = "deleteChild", value = "是否同时删除子组织", paramType = "query", allowableValues = "true,false")
    })
    @OpsLog(value = "删除组织", type = OpsLogTypeEnum.DELETE)
    @OperLog(title = "组织架构-删除组织", content = "删除组织")
    @PostMapping("delete")
    public BaseResponse<ErrorMsgVO> deleteHosOrganization(@NotBlank(message = "不能为空") String orgCode, @RequestParam(defaultValue = "false") boolean deleteChild) {
        ErrorMsgVO msgVO = this.hosOrganizationService.deleteHosOrganization(orgCode, deleteChild);
        return BaseResponse.success(msgVO);
    }

    /*@ApiOperation(value = "导出组织")
    @ApiOperationSupport(order = 9)
    @OpsLog(value = "导出组织", type = OpsLogTypeEnum.DOWNLOAD)
    @OperLog(title = "组织架构-导出组织", content = "导出组织")
    @GetMapping("export")
    public AjaxResult export(HosOrganization hosOrganization) {
        List<HosOrganization> types = ((Page<HosOrganization>) this.hosOrganizationService.findHosOrganization(hosOrganization, false, new QueryRequest(1, Integer.MAX_VALUE))).getRecords();
        ExcelUtil<HosOrganization> util = new ExcelUtil<>(HosOrganization.class);
        return util.exportExcel(types, "组织");
    }

    @ApiOperation(value = "导入组织")
    @ApiOperationSupport(order = 11)
    @OpsLog(value = "导入组织", type = OpsLogTypeEnum.UPLOAD)
    @OperLog(title = "组织架构-导入组织", content = "导入组织")
    @PostMapping("import")
    public R<HosOrganization> uploadHierarchy(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<HosOrganization> util = new ExcelUtil<>(HosOrganization.class);
        List<HosOrganization> hosOrganizations = util.importExcel(file.getInputStream());
        ErrorMsgVO result = this.hosOrganizationService.importHosOrganization(hosOrganizations, updateSupport);
        return R.success(JSONUtil.toJsonStr(result));
    }

    @ApiOperation(value = "导出组织模板")
    @ApiOperationSupport(order = 13)
    @OpsLog(value = "导出组织模板", type = OpsLogTypeEnum.DOWNLOAD)
    @Log(title = "导出组织模板", businessType = BusinessType.EXPORT)
    @GetMapping("template")
    @ResponseBody
    public AjaxResult hierarchyTemplate() {
        ExcelUtil<HosOrganization> util = new ExcelUtil<>(HosOrganization.class);
        return util.importTemplateExcel("组织模板");
    }
*/
    /*@Log(title = "组织的拆分与合并", businessType = BusinessType.UPDATE)
    @OpsLog(value = "组织的拆分与合并", type = OpsLogTypeEnum.UPDATE)
    @PostMapping("hierarchy/merge-split")
    @ApiOperation(value = "组织的拆分与合并")
    @ApiOperationSupport(order = 15)
    public R<String> mergeSplit(@RequestBody DeHierarchyOperation deptOperation) {
        this.deHierarchyOperationService.mergeSplit(deptOperation);
        return R.success();
    }*/
}

